<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户姓名" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入用户真实姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input v-model="queryParams.phonenumber" placeholder="请输入中国大陆手机号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="是否认证" prop="identityAuth">
        <el-input v-model="queryParams.identityAuth" placeholder="请输入是否已完成身份及车辆认证" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->

      <!-- <el-form-item label="车牌号" prop="lpn">
        <el-input v-model="queryParams.lpn" placeholder="请输入车牌号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:externalUser:add']">新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:externalUser:edit']">修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:externalUser:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:externalUser:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="externalUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户昵称" align="center" prop="userName" />
      <!-- <el-table-column label="真实姓名" align="center" prop="nickName" /> -->
      <el-table-column label="用户类型" align="center" prop="userType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.external_user_type" :value="scope.row.userType" />
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phonenumber" />
      <el-table-column label="身份及车辆认证" align="center" prop="identityAuth">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.identity_user_auth" :value="scope.row.identityAuth" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="身份证号" align="center" prop="idNumber" /> -->
      <!-- <el-table-column label="身份证正面照" align="center" prop="idImgUrl" /> -->
      <!-- <el-table-column label="车牌号" align="center" prop="lpn" /> -->
      <!-- <el-table-column label="车架号" align="center" prop="vin" /> -->
      <!-- <el-table-column label="车辆行驶证正面照" align="center" prop="drivingLicenseImgUrl" /> -->
      <el-table-column label="积分余额" align="center" prop="pointsBalance" />
      <el-table-column label="充值余额" align="center" prop="rechargeBalance" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-s-custom" @click="handleAuthInfo(scope.row)"
            v-if="scope.row.identityAuth == true" v-hasPermi="['business:externalUser:edit']">认证信息</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:externalUser:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:externalUser:remove']" style="color: red;">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改充电用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户昵称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户昵称" />
        </el-form-item>
        <!-- <el-form-item label="真实姓名" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户真实姓名" />
        </el-form-item> -->
        <el-form-item label="手机号" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入中国大陆手机号" />
        </el-form-item>
        <el-form-item label="是否已认证" prop="identityAuth">
          <el-input v-model="form.identityAuth" placeholder="请输入是否已完成身份及车辆认证" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <!-- <el-form-item label="身份证正面照" prop="idImgUrl">
          <el-input v-model="form.idImgUrl" placeholder="请输入身份证正面照" />
        </el-form-item> -->
        <!-- <el-form-item label="车牌号" prop="lpn">
          <el-input v-model="form.lpn" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车架号" prop="vin">
          <el-input v-model="form.vin" placeholder="请输入车架号" />
        </el-form-item> -->
        <!-- <el-form-item label="车辆行驶证正面照" prop="drivingLicenseImgUrl">
          <el-input v-model="form.drivingLicenseImgUrl" placeholder="请输入车辆行驶证正面照" />
        </el-form-item> -->
        <!-- <el-form-item label="积分余额" prop="pointsBalance">
          <el-input v-model="form.pointsBalance" placeholder="请输入积分余额，默认0，最大99999999" />
        </el-form-item>
        <el-form-item label="充值余额" prop="rechargeBalance">
          <el-input v-model="form.rechargeBalance" placeholder="请输入充值余额，默认0.00" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户认证信息 -->
    <el-dialog :title="title" :visible.sync="openView" width="40%" append-to-body>
      <el-form ref="form" :model="form" label-width="130px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户昵称：">{{ form.userName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户类型："><dict-tag :options="dict.type.external_user_type"
                :value="form.userType" /></el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="真实姓名：">{{ form.nickName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号：">{{ form.phonenumber }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证正面照：">{{ form.idImgUrl }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份及车辆认证：">
              <dict-tag :options="dict.type.identity_user_auth" :value="form.identityAuth" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号：">{{ form.idNumber }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车牌号：">{{ form.lpn }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车架号：">{{ form.vin }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行驶证正面照：">{{ form.drivingLicenseImgUrl }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="积分余额：">{{ form.pointsBalance }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="充值余额：">{{ form.rechargeBalance }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExternalUser, getExternalUser, delExternalUser, addExternalUser, updateExternalUser } from "@/api/business/externalUser"

export default {
  name: "ExternalUser",
  dicts: ['external_user_type', 'identity_user_auth'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充电用户表格数据
      externalUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 认证信息框
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        nickName: null,
        userType: null,
        phonenumber: null,
        identityAuth: null,
        idNumber: null,
        idImgUrl: null,
        lpn: null,
        vin: null,
        drivingLicenseImgUrl: null,
        pointsBalance: null,
        rechargeBalance: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        userType: [
          { required: true, message: "用户类型不能为空", trigger: "change" }
        ],
        phonenumber: [
          { required: true, message: "中国大陆手机号不能为空", trigger: "blur" }
        ],
        identityAuth: [
          { required: true, message: "是否已完成身份及车辆认证不能为空", trigger: "blur" }
        ],
        pointsBalance: [
          { required: true, message: "积分余额，默认0，最大99999999不能为空", trigger: "blur" }
        ],
        rechargeBalance: [
          { required: true, message: "充值余额，默认0.00不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "用户创建时间不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询充电用户列表 */
    getList() {
      this.loading = true
      listExternalUser(this.queryParams).then(response => {
        this.externalUserList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        userName: null,
        nickName: null,
        userType: null,
        phonenumber: null,
        identityAuth: null,
        idNumber: null,
        idImgUrl: null,
        lpn: null,
        vin: null,
        drivingLicenseImgUrl: null,
        pointsBalance: null,
        rechargeBalance: null,
        createTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加充电用户"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const userId = row.userId || this.ids
      getExternalUser(userId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改充电用户"
      })
    },
    // 认证信息
    handleAuthInfo(row) {
      this.reset()
      const userId = row.userId || this.ids
      getExternalUser(userId).then(response => {
        this.form = response.data
        this.openView = true
        this.title = "用户身份及车辆认证信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateExternalUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addExternalUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids
      this.$modal.confirm('是否确认删除充电用户编号为"' + userIds + '"的数据项？').then(function () {
        return delExternalUser(userIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/externalUser/export', {
        ...this.queryParams
      }, `externalUser_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
